from socket import *

HOST = "127.0.0.1"
PORT = 9001
ADDR = (HOST, PORT)


class TcpClient:
    def __init__(self, addr):
        self.addr = addr
        self.sock = self._create()

    def _create(self):
        sock = socket()
        sock.connect(self.addr)
        return sock


class View:
    def __init__(self):
        self.controller = Controller()
        self.user = ""

    def menu_1(self):
        while True:
            print("""
            ===========================================
            1 登录          2 注册            3 退出
            ===========================================
            """)
            msg = input(">>")
            if msg == "1":
                self.__login()
            elif msg == "2":
                self.__enroll()
            elif msg == "3":
                print("退出成功")
                break
            else:
                print("输入错误\n")

    def menu_2(self):
        while True:
            print("""
            ===========================================
            1 查单词          2 历史记录            3 注销
            ===========================================
            """)
            msg = input(">>")
            if msg == "1":
                self.__find()
            elif msg == "2":
                self.__logs()
            elif msg == "3":
                break
            else:
                print("输入错误\n")

    def __login(self):
        user = input("请输入用户名：")
        password = input("请输入密码：")
        response = self.controller.login(user, password)
        if response == "1":
            self.user = user
            self.menu_2()

        elif response == "2":
            print("密码错误")
        elif response == "3":
            print("用户不存在")

    def __enroll(self):
        user = input("请输入用户名：")
        password = input("请输入密码：")
        response = self.controller.enroll(user, password)
        if response == "1":
            print("注册成功")
        elif response == "2":
            print("用户已存在")

    def __find(self):
        while True:
            word = input("输入要查找的单词：")
            response = self.controller.find(self.user, word)
            if response == "##":
                print("退出查询")
                break
            elif response != "2":
                print(response)
            else:
                print("无此单词")

    def __logs(self):
        response = self.controller.logs(self.user)
        if response == "2":
            print("日志为空")
        else:
            print(response)

    def __logout(self):
        password = input("请输入密码：")


class Controller:
    def __init__(self):
        self.sock = TcpClient(ADDR).sock

    def login(self, user, password):
        msg = f"login:{user}:{password}"
        data = self.interaction(msg)
        return data

    def enroll(self, user, password):
        msg = f"enroll:{user}:{password}"
        data = self.interaction(msg)
        return data

    def find(self, user, word):
        msg = f"find:{user}:{word}"
        data = self.interaction(msg)
        return data

    def logs(self, user):
        msg = f"logs:{user}"
        data = self.interaction(msg)
        return data

    def interaction(self, msg):
        self.sock.send(msg.encode())
        data = self.sock.recv(1024).decode()
        return data


if __name__ == '__main__':
    v = View()
    v.menu_1()
